//
//  TitleBarView.m
//  Cunli
//
//  Created by lilubin on 15/6/9.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "TitleBarView.h"
#import "UIColor+expanded.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface TitleBarView ()
{
    UIView *_line;
}

@end

@implementation TitleBarView

- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    
    if (self) {
        _currentIndex = 0;
        _titleButtons = [NSMutableArray arrayWithCapacity:5];
        
        CGFloat btnWidth = frame.size.width * 1.0 / titles.count;
        CGFloat btnHeight = frame.size.height;
        
        
        //便民模块大于5个
        if (titles.count>5) {
            btnWidth = frame.size.width * 1.0 / 4.5;
        }
        
        [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            UIColor *bgColor = [UIColor colorWithWhite:1.0 alpha:0.9];
            [button setBackgroundColor:bgColor];
            [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            UIColor *selColor = [UIColor colorWithHexString:@"0xed5955"];
            [button setTitleColor:selColor forState:UIControlStateSelected];
            [button setTitle:title forState:UIControlStateNormal];
            button.frame = CGRectMake(btnWidth*idx, 0, btnWidth, btnHeight);
            button.tag = idx;
            [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
            if (idx == 0) {
                [button setTitleColor:selColor forState:UIControlStateNormal];
            }
            
            [_titleButtons addObject:button];
            [self addSubview:button];
            [self sendSubviewToBack:button]; //这个是做什么用的
        }];
        _currentIndex = 0;
        
        self.contentSize = CGSizeMake(frame.size.width, 25);
        self.showsHorizontalScrollIndicator = NO;
        _line = [[UIView alloc] initWithFrame:CGRectMake(2.0f, frame.size.height - 2.0f, kScreenWidth*1.0/titles.count - 4.0f, 2.0f)];
        [_line setBackgroundColor:[UIColor colorWithHexString:@"0xed5955"]];
        _line.tag = 1050;
        [self addSubview:_line];
        
        //便民模块大于5个
        if (titles.count>5) {
            self.contentSize = CGSizeMake((frame.size.width/4.5)*6, 25);
            self.bounces=NO;
        }
    }
    return self;
}

- (void)onClick:(UIButton *)button
{
    if (_currentIndex != button.tag) {
        UIButton *preTitle = _titleButtons[_currentIndex];
        
        CGFloat itemWidth = kScreenWidth * 1.0 / _titleButtons.count;
        UIColor *selColor = [UIColor colorWithHexString:@"0xed5955"];
        
        [preTitle setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:selColor forState:UIControlStateNormal];
        _currentIndex = button.tag;
        _titleButtonClicked(button.tag);
        
        /*
         if (_line == nil) {
         _line = [[UIView alloc] initWithFrame:CGRectMake(itemWidth * _currentIndex + 2.0f, self.frame.size.height - 2.0f, itemWidth-4.0f, 2.0f)];
         [_line setBackgroundColor:selColor];
         [self addSubview:_line];
         }
         */
        [UIView animateWithDuration:0.2 animations:^{
            [_line setFrame:CGRectMake(itemWidth * _currentIndex + 2.0f, self.frame.size.height - 2.0f, itemWidth-4.0f, 2.0f)];
        }];
        //便民里设置偏移量
        if (_titleButtons.count>5) {
            
            float buttonPositn=button.frame.origin.x+button.frame.size.width;
            if (buttonPositn>self.frame.size.width) {
                [UIView animateWithDuration:0.5 animations:^{
                    [self setContentOffset:CGPointMake(buttonPositn-self.frame.size.width+button.frame.size.width*(5-button.tag), 0)];
                }];
            }
            if (button.tag==0||button.tag==1) {
                [UIView animateWithDuration:0.5 animations:^{
                    [self setContentOffset:CGPointMake(0, 0)];
                }];
            }
        }
    }
}


@end
